﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using Hsinchu.Model;


namespace Hsinchu.BLL
{
    /*
     * 类别业务逻辑
     */
    public class CategoryMgr
    {
        DAL.DataProcess dllDataProcess = new Hsinchu.DAL.DataProcess();


        /// <summary>
        /// 添加新类别
        /// </summary>
        /// <param name="category">类别对象</param>
        public void AddCategory(Model.MCategory category)
        {
            string _spName = "Insert_Category";
            object[] _value = new object[2];
            _value[0] = category.CategoryName;
            _value[1] = category.ParentCategoryId;
            dllDataProcess.getAffectRows(_spName, _value);
        }

        /// <summary>
        /// 根据类别父id递归查询子类别
        /// </summary>
        /// <param name="ParentCategoryId">父类别id</param>
        /// <returns>子类别Dictionary</returns>
        public Dictionary<string, string> GetCategoryByParentCategoryId(int ParentCategoryId)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("|----根类别", "0");
            string _spName = "Select_CategoryByParentCategoryId";
            object[] _value = new object[1];
            _value[0] = ParentCategoryId;
            DataTable dtCategory = dllDataProcess.getDataTable(_spName, _value);
            for (int i = 0; i < dtCategory.Rows.Count; i++)
            {
                dic.Add("|----" + dtCategory.Rows[i]["CategoryName"].ToString(), dtCategory.Rows[i]["CategoryId"].ToString());
                //递归
                Recursive(Convert.ToInt32(dtCategory.Rows[i]["CategoryId"].ToString()), "|----", dic);
            }
            return dic;
        }

        /// <summary>
        /// 递归方法
        /// </summary>
        /// <param name="parentcategoryid">父类别id</param>
        /// <param name="prefix">前缀字符串</param>
        /// <param name="dic">Dictionary结果集</param>
        private void Recursive(int parentcategoryid, string prefix, Dictionary<string, string> dic)
        {
            string _spName = "Select_CategoryByParentCategoryId";
            object[] _value = new object[1];
            _value[0] = parentcategoryid;
            DataTable dtCategory = dllDataProcess.getDataTable(_spName, _value);
            for (int i = 0; i < dtCategory.Rows.Count; i++)
            {
                dic.Add(prefix + "----" + dtCategory.Rows[i]["CategoryName"].ToString(), dtCategory.Rows[i]["CategoryId"].ToString());

                //递归
                Recursive(Convert.ToInt32(dtCategory.Rows[i]["CategoryId"].ToString()), prefix + "----", dic);
            }
        }
    }
}
